package com.koron.standard.deviceClasssify.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.deviceClasssify.bean.po.TypeGroupParams;
import com.koron.standard.deviceClasssify.mapper.TypeGroupParamsMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 设备分类基本参数组参数(TypeGroupParams)表服务实现类
 *
 * @author jzp
 * @since 2021-07-13 11:13:26
 */
@Service
public class TypeGroupParamsService extends ServiceImpl<TypeGroupParamsMapper, TypeGroupParams> {

    /**
     * 查询出所有的初始预设参数
     */
    public List<TypeGroupParams> listDefault() {
        LambdaQueryWrapper<TypeGroupParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(TypeGroupParams::getGroupId);
        wrapper.eq(TypeGroupParams::getIsChange, 0);
        wrapper.orderByAsc(TypeGroupParams::getSort);
        List<TypeGroupParams> list = list(wrapper);
        // id置空
        list.forEach(e -> e.setId(null));
        return list;
    }

    /**
     * 查询出该组所有的预设参数
     */
    public List<TypeGroupParams> listDefaultByGroupId(String groupId) {
        LambdaQueryWrapper<TypeGroupParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeGroupParams::getGroupId, groupId);
        wrapper.eq(TypeGroupParams::getIsChange, 0);
        wrapper.orderByAsc(TypeGroupParams::getSort);
        return list(wrapper);
    }

    /**
     * 查询出该组所有的自定义参数
     */
    public List<TypeGroupParams> listCustomByGroupId(String groupId) {
        LambdaQueryWrapper<TypeGroupParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeGroupParams::getGroupId, groupId);
        wrapper.eq(TypeGroupParams::getIsChange, 1);
        wrapper.orderByAsc(TypeGroupParams::getSort);
        return list(wrapper);
    }

    /**
     * 保存基本参数
     */
    public void saveGroup(Collection<TypeGroupParams> typeGroupDefaultParamsList, String groupId) {
        // 参数名称唯一
        long count = typeGroupDefaultParamsList.stream().map(TypeGroupParams::getParamName).distinct().count();
        if (typeGroupDefaultParamsList.size() > count) {
            throw new IllegalArgumentException("参数名重复");
        }

        removeByGroupId(groupId);
        // 2.
        AtomicInteger sortNo = new AtomicInteger(0);
        for (TypeGroupParams typeGroupParams : typeGroupDefaultParamsList) {
            if (typeGroupParams.getGroupId() == null) {
                typeGroupParams.setGroupId(groupId);
            }
            typeGroupParams.setSort(sortNo.getAndIncrement());
        }

        // 3. 插入
        saveBatch(typeGroupDefaultParamsList);
    }

    /**
     * 删除该组的所有基本参数
     */
    public void removeByGroupId(String groupId) {
        if (StrUtil.isEmpty(groupId)) {
            return;
        }
        LambdaQueryWrapper<TypeGroupParams> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeGroupParams::getGroupId, groupId);
        List<TypeGroupParams> list = list(wrapper);
        removeByIds(list.stream().map(TypeGroupParams::getId).collect(Collectors.toList()));
    }

}